package com.itheima.web.controller.system;

import com.itheima.common.utils.UtilFuns;
import com.itheima.doamin.system.Role;
import com.itheima.doamin.system.User;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.UserService;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController{
    /**
     *fenye 查询所有
     */
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "3") int size){
        String companyId="1";
        request.setAttribute("page",userService.findAll(companyId,page,size));
        return "system/user/user-list";
    }

    //添加
    @RequestMapping("toAdd")
    public String toAdd(){
        List<User> deptList = userService.findAll(super.getCurrentUser().getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    //更新
    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);
        List<User> userList = userService.findAll(super.getCurrentUser().getCompanyId());
        request.setAttribute("userList",userList);
        return "system/user/user-update";
    }

    /**
     * 进行更新或则添加操作
     * @param user
     * @return
     */
    @RequestMapping("edit")
    public String edit(User user) throws Exception {
        if (UtilFuns.isEmpty(user.getId())){
            //为空则保存
            user.setCompanyId(super.getCurrentUser().getCompanyId());
            user.setCompanyName(super.getCurrentUser().getCompanyName());
            userService.save(user);
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list";
    }

    //删除
    @RequestMapping("delete")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/system/user/list";
    }

    //用户角色权限的回显
    @RequestMapping("roleList")
    public String roleList(String id){
        //根据id查询用户
        User user = userService.findById(id);
        //权限列表
        List<Role> roleList = roleService.findAll(super.getCurrentUser().getCompanyId());
        //用户拥有的权限列表id,查询中间表
        List<String> userRoleStr=userService.userRoleStr(id);
        request.setAttribute("user",user);
        request.setAttribute("roleList",roleList);
        request.setAttribute("userRoleStr",userRoleStr);
        return "system/user/user-role";
    }

    /**
     * 角色权限改变
     */
    @RequestMapping("changeRole")
    public  String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list";
    }
}
